import { cozeInstance } from '../../utils/request.js';
import { Config } from '../../config.js'; // 导入配置

// 使用配置文件中的bot_id
const bot_id = Config.COZE.BOT_ID;
const role = 'user';

/**
 * 创建对话 (Chat V3)
 * @param {object} data - { user_id }
 */
export const createChatAPI = (data) => {
  return cozeInstance({
    url: '/v3/chat',
    method: 'POST',
    data: { 
      bot_id,
      user_id: data.user_id,
      conversation_mode: 'single'
    }
  });
};

/**
 * 发送消息 (Chat V3)
 * @param {object} data - { conversation_id, user_id, content, stream = false }
 */
export const sendChatMessageAPI = (data) => {
  const { conversation_id, user_id, content, stream = false } = data;
  return cozeInstance({
    url: '/v3/chat/message',
    method: 'POST',
    data: { 
      bot_id,
      conversation_id,
      user_id,
      content,
      content_type: 'text',
      stream
    }
  });
};

/**
 * 获取对话详情 (Chat V3)
 * @param {object} data - { conversation_id, user_id }
 */
export const getChatDetailAPI = (data) => {
  const { conversation_id, user_id } = data;
  return cozeInstance({
    url: `/v3/chat/${conversation_id}`,
    method: 'GET',
    params: { bot_id, user_id }
  });
};

/**
 * 获取对话消息列表 (Chat V3)
 * @param {object} data - { conversation_id, user_id, limit = 20, last_id }
 */
export const getChatMessagesAPI = (data) => {
  const { conversation_id, user_id, limit = 20, last_id } = data;
  return cozeInstance({
    url: `/v3/chat/${conversation_id}/messages`,
    method: 'GET',
    params: { bot_id, user_id, limit, last_id }
  });
};

/**
 * 删除对话 (Chat V3)
 * @param {object} data - { conversation_id, user_id }
 */
export const deleteChatAPI = (data) => {
  const { conversation_id, user_id } = data;
  return cozeInstance({
    url: `/v3/chat/${conversation_id}`,
    method: 'DELETE',
    params: { bot_id, user_id }
  });
};

/**
 * 创建会话
 * @param {object} data - { user_id }
 */
export const createConversationAPI = (data) => {
  return cozeInstance({
    url: '/v1/conversation/create',
    method: 'POST',
    data: { bot_id, ...data }
  });
};

/**
 * 获取会话列表
 * @param {object} data - { user_id }
 */
export const getConversationListAPI = (data) => {
  return cozeInstance({
    url: '/conversation/list',
    method: 'GET',
    params: { bot_id, ...data }
  });
};

/**
 * 发送消息
 * @param {object} data - { conversation_id, user_id, content, content_type = 'text' }
 */
export const sendMessageAPI = (data) => {
  const { conversation_id, user_id, content, content_type = 'text' } = data;
  return cozeInstance({
    url: '/v1/conversation/message/create',
    method: 'POST',
    data: { user_id, role, content, content_type },
    params: { conversation_id }
  });
};

/**
 * 获取消息历史
 * @param {object} data - { conversation_id, limit = 20 }
 */
export const getMessageHistoryAPI = (data) => {
  return cozeInstance({
    url: '/v1/conversation/message/list',
    method: 'GET',
    params: { ...data, bot_id }
  });
};

/**
 * 查看会话消息 (会话检索)
 * @param {object} data - { conversation_id, user_id }
 */
export const retrieveConversationAPI = (data) => {
  const { conversation_id, user_id } = data;
  return cozeInstance({
    url: `/v1/conversation/retrieve`,
    method: 'GET',
    params: { bot_id, conversation_id, user_id }
  });
};

/**
 * 获取会话详细信息 (包含消息历史)
 * @param {object} data - { conversation_id, user_id, limit = 20, last_id }
 */
export const getConversationDetailsAPI = (data) => {
  const { conversation_id, user_id, limit = 20, last_id } = data;
  return cozeInstance({
    url: `/v1/conversation/${conversation_id}/details`,
    method: 'GET',
    params: { bot_id, user_id, limit, last_id }
  });
};